﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BusinessLayer.RegInfo.Models;
using DBHelper;
using BusinessLayer.RegInfo.Entities;
using System.Data.SqlClient;
using System.Data;


namespace BusinessLayer.RegInfo
{
    public class AdminMenuBL
    {
        public List<AdminMenuExtend> GetAll()
        {            
            var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
            var list = adapter.GetList<AdminMenuExtend>(Common.Constants.StoredProcedures.GetAllAdminMenu, CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
            return list;
        }
        public int CreateAdminMenu(AdminMenu menu)
        {
            int result = -1;
            try
            {
                var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
                adapter.AddParameter("@Code", menu.Code);
                adapter.AddParameter("@Name", menu.Name);
                adapter.AddParameter("@Url", menu.Url);
                adapter.AddParameter("@ParentID", menu.ParentID);
                adapter.AddParameter("@OrderNo", menu.OrderNo);

                result = adapter.ExecuteNonQuery(Common.Constants.StoredProcedures.InsertAdminMenu, CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
                return result;
            }
            catch (Exception ex)
            {

            }
            return result;


        }
        public int UpdateAdminMenu(AdminMenu menu)
        {
            int result = -1;
            try
            {
                var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
                adapter.AddParameter("@MenuID", menu.MenuID);
                adapter.AddParameter("@Code", menu.Code);
                adapter.AddParameter("@Name", menu.Name);
                adapter.AddParameter("@Url", menu.Url);
                adapter.AddParameter("@ParentID", menu.ParentID);
                adapter.AddParameter("@OrderNo", menu.OrderNo);

                result = adapter.ExecuteNonQuery(Common.Constants.StoredProcedures.UpdateAdminMenu, CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
                return result;
            }
            catch (Exception ex)
            {

            }
            return result;


        }

        public AdminMenu GetByMenuCode(string code)
        {            
            var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
            adapter.AddParameter("@Code", code);
            var list = adapter.GetList<AdminMenu>(Common.Constants.StoredProcedures.GetAdminMenuByCode, CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
            return list.FirstOrDefault();
        }
        public AdminMenu GetByMenuID(int id)
        {            
            var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
            adapter.AddParameter("@MenuID", id);
            var list = adapter.GetList<AdminMenu>(Common.Constants.StoredProcedures.GetAdminMenuByID, CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
            return list.FirstOrDefault();
        }
        public int DeleteAdminMenuByIds(List<string> lstofids)
        {
            int result = -1;

            string ids = "";
            foreach (var id in lstofids)
            {
                ids += id + ",";
            }
            ids = String.IsNullOrEmpty(ids) ? "" : ids.Substring(0, ids.Length - 1);
            if (!String.IsNullOrEmpty(ids))
            {
                try
                {
                    var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
                    result = adapter.ExecuteNonQuery(String.Format("DELETE FROM AdminMenu WHERE MenuID IN ({0});", ids), CommandType.Text, CustomConnectionState.CloseOnExit);
                    return result;
                }
                catch (Exception ex)
                {

                }
            }
            return result;
        }
        //GetAdminMenuByMenuGroupID
        public List<AdminMenu> GetAdminMenuByUserGroupID(int usergroupid)
        {            
            var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
            adapter.AddParameter("@UserGroupID", usergroupid);
            var list = adapter.GetList<AdminMenu>(Common.Constants.StoredProcedures.GetAdminMenuByUserGroupID, CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
            return list;
        }
        //sp_AdminMenu_Select_By_UserID
        public List<AdminMenu> GetAdminMenuByUserID(int userid)
        {            
            var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
            adapter.AddParameter("@UserID", userid);
            var list = adapter.GetList<AdminMenu>(Common.Constants.StoredProcedures.GetAdminMenuByUserID, CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
            return list;
        }
        //  sp_AdminMenu_Select_By_UserName
        public List<AdminMenuExtend> GetAdminMenuByUserName(string username)
        {            
            var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
            adapter.AddParameter("@UserName", username);
            var list = adapter.GetList<AdminMenuExtend>(Common.Constants.StoredProcedures.GetAdminMenuByUserName, CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
            return list;
        }
    }
}
